﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using devtm.Editor.TypeSelector;
using devtm.Collections;
using Microsoft.VisualStudio.Modeling;
using devtm.AutoMapper.CustomCode.TemplateAdd;
using Mono.Cecil;

namespace devtm.AutoMapper
{
    public class ViewNamespaceSelectEditor : UITypeEditor
    {

        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            //return base.EditValue(context, provider, value);

            // Return the value if the value is not of type Int32, Double and Single.
            //if (value.GetType() != typeof(double) && value.GetType() != typeof(float) && value.GetType() != typeof(int))
            //    return value;

            // Uses the IWindowsFormsEditorService to display a 
            // drop-down UI in the Properties window.
            IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            if (edSvc != null)
            {

                UsingNamespace Instance;

                if (context.Instance.GetType().IsArray)
                    Instance = ((Object[])context.Instance)[0] as UsingNamespace;
                else
                    Instance = context.Instance as UsingNamespace;

                if (Instance != null)
                {

                    Store store = Instance.Store;

                    using (CustomTypeRepository repository = CustomTypeRepository.Instance(store, false))
                    {
                        TypeElementShape g = context.Instance as TypeElementShape;

                        WindowsSelectType frm = null;

                        try
                        {
                            frm = new WindowsSelectType();
                        }
                        catch (System.Exception)
                        {
                            return value;
                        }


                        try
                        {

                            frm.SetList(repository, null, value as string, false);

                            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                                value = frm.SelectedItem as string;

                        }
                        catch (Exception e)
                        {

                            Helpers._output.WriteLine(e.ToString());
                            return value;

                        }

                    }

                }
            }

            return value;


        }

        public override bool IsDropDownResizable
        {
            get
            {
                return false;
            }
        }

        public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {

            UsingNamespace Instance;

            if (context.Instance.GetType().IsArray)
                Instance = ((Object[])context.Instance)[0] as UsingNamespace;
            else
                Instance = context.Instance as UsingNamespace;

            if (Instance == null)
                return UITypeEditorEditStyle.None;

            return UITypeEditorEditStyle.Modal;
            //return base.GetEditStyle(context);
        }

    }
}
